package littlepicture.data.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	private static Calendar calendar = Calendar.getInstance();
	
	public static Date createDateFromString(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public static int getDayOfWeek(Date date) {
		calendar.clear();
		calendar.setTime(date);
		return 
		  calendar.get(Calendar.DAY_OF_WEEK) - 1;
	}
	
	public static Date getFirstDateOfWeek(Date date) {
		calendar.clear();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)
				- calendar.get(Calendar.DAY_OF_WEEK) + 1);
		return calendar.getTime();
	}
	
	public static Date getLastDateOfWeek(Date date) {
		calendar.clear();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)
				+ Calendar.DAY_OF_WEEK - calendar.get(Calendar.DAY_OF_WEEK));
		return calendar.getTime();
	}
	
	public static Date getNextDay(Date date){
		calendar.clear();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}
	
	public static void main(String[] args) {
		Date date = createDateFromString("2009-12-31");
		System.out.println(getNextDay(date));
	}
}
